/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package anton.admin.reservation;

import anton.admin.reservation.Reservation;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.sql.DataSource;

/**
 *
 * @author Chris
 */
@ManagedBean(name = "destroyReservation")
@SessionScoped

public class DestroyReservation implements Serializable {
    
    @Resource(name = "jdbc/antonsDatasource") // data source to use for mysql queries
    private DataSource ds;
    int stat;
    ArrayList<Reservation> list;
    public ArrayList<Reservation> getList() {
        return list;
    }
    public void setList(ArrayList<Reservation> list) {
        this.list = list;
    }
    Reservation Reservation;
    public void setDs(DataSource ds) {
        this.ds = ds;
    }
    public String destroy(int reservationId) throws SQLException {
        String sql;// variable for sql statements
        String target = "/admin/reservations?faces-redirect=true";

        try (Connection con = ds.getConnection()) {
            sql = "delete from Reservations where reservation_ID =" + "'" + reservationId + "'";

            PreparedStatement ps; // query delete items
            ps = con.prepareStatement(sql);

            stat = ps.executeUpdate();
            sql = "delete from Reservations where reservation_ID =" + "'" + reservationId + "'";

            ps = con.prepareStatement(sql);

            stat = ps.executeUpdate();

            // removes deleted items from list
        }
        return target;
    }
}
